package cn.cdtu.tibetplan.service.impl;

import cn.cdtu.tibetplan.mapper.DemandMapper;
import cn.cdtu.tibetplan.pojo.Demand;
import cn.cdtu.tibetplan.pojo.DemandExample;
import cn.cdtu.tibetplan.pojo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.cdtu.tibetplan.pojo.DemandExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;

import cn.cdtu.tibetplan.service.DemandService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class DemandServiceImpl implements DemandService {

    @Autowired
    private DemandMapper demandMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Demand> findAll() {
        return demandMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Demand> page = (Page<Demand>) demandMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Demand demand) {
        demandMapper.insert(demand);
    }


    /**
     * 修改
     */
    @Override
    public void update(Demand demand) {
        demandMapper.updateByPrimaryKeySelective(demand);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Demand findOne(Long id) {
        return demandMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            demandMapper.deleteByPrimaryKey(id.intValue());
        }
    }

    @Override
    public PageResult findPage(Demand demand, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        DemandExample example = new DemandExample();
        Criteria criteria = example.createCriteria();

        if (demand != null) {
            if (demand.getTitle() != null && demand.getTitle().length() > 0) {
                criteria.andTitleLike("%" + demand.getTitle() + "%");
            }
            if (demand.getInfo() != null && demand.getInfo().length() > 0) {
                criteria.andInfoLike("%" + demand.getInfo() + "%");
            }
            if (demand.getStatus() != null && demand.getStatus().length() > 0) {
                criteria.andStatusLike("%" + demand.getStatus() + "%");
            }
            if (demand.getSchoolname() != null && demand.getSchoolname().length() > 0) {
                criteria.andSchoolnameLike("%" + demand.getSchoolname() + "%");
            }
            if (demand.getOperator() != null && demand.getOperator().length() > 0) {
                criteria.andOperatorLike("%" + demand.getOperator() + "%");
            }
            if (demand.getCreatetime() != null && demand.getCreatetime().length() > 0) {
                criteria.andCreatetimeLike("%" + demand.getCreatetime() + "%");
            }
            if (demand.getUpdatetime() != null && demand.getUpdatetime().length() > 0) {
                criteria.andUpdatetimeLike("%" + demand.getUpdatetime() + "%");
            }
            if (demand.getOther3() != null && demand.getOther3().length() > 0) {
                criteria.andOther3Like("%" + demand.getOther3() + "%");
            }

        }
        Page<Demand> page = (Page<Demand>) demandMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }
}
